/**
 * 
 */
package com.taobao.api.model;

import java.io.Serializable;
import java.util.Map;

import com.taobao.api.util.EncodeUtil;

/**
 * 调用Top的API返回的response ErrorCode错误码 body 响应的body部分 redirectUrl 绑定用户所需重定向URL
 * body 响应的body部分
 * 
 * @author alin
 * @author sulinchong.pt
 * 
 */
public class TaobaoResponse implements Serializable {
	private static final long serialVersionUID = 5014379068811962022L;

	private String errorCode;
	private String msg;
	private String redirectUrl;
	private String body;
	private Map<String, String> headers;
	/*
	 * 添加requestUrl和requestBody
	 */
	private String requestUrl;
	private String requestBody;
	private String requestParameters;

	public String getHeader(String filedName) {
		if (this.headers == null) {
			return null;
		}
		return this.headers.get(filedName);
	}

	public Map<String, String> getHeaders() {
		return headers;
	}

	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}

	public TaobaoResponse() {
		super();
	}

	private TaobaoResponse(String body, String errorCode, String msg,
			String redirectUrl, String requestUrl, String requestBody, String requestParameters) {
		super();
		this.body = body;
		this.errorCode = errorCode;
		this.msg = msg;
		this.redirectUrl = redirectUrl;
		this.requestUrl = requestUrl;
		this.requestParameters = requestParameters;
		this.requestBody = requestBody;
	}

	public TaobaoResponse(TaobaoResponse rsp) {
		this(rsp.body, rsp.errorCode, rsp.msg, rsp.redirectUrl, rsp.requestUrl, rsp.requestBody, rsp.requestParameters);
	}

	public String getErrorCode() {
		return errorCode;
	}

	/**
	 * 判断是否调用API成功
	 * 
	 * @return 如果成功,返回true
	 */
	public boolean isSuccess() {
		return null == errorCode;
	}

	public boolean isRedirect() {
		return null != redirectUrl;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 得到绑定用户需要重定向的URL
	 * 
	 * @return
	 */
	public String getRedirectUrl() {
		return redirectUrl;
	}

	/**
	 * 得到绑定用户需要重定向的URL
	 * 
	 * @param target
	 *            绑定用户成功后重定向的目标地址
	 * @return
	 */
	public String getRedirectUrl(String target) {
		return redirectUrl + "&sip_redirecturl=" + target;
	}

	/**
	 * 得到绑定用户需要重定向的URL，对绑定后重定向回isv的url做urlEncode编码
	 * 
	 * @param target
	 * @return
	 */
	public String getEncodedRedirectUrl(String target) {
		return redirectUrl + "&sip_redirecturl=" + EncodeUtil.encodeUrl(target);
	}

	public void setRedirectUrl(String redirectUrl) {
		this.redirectUrl = redirectUrl;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getRequestUrl() {
		return requestUrl;
	}

	public void setRequestUrl(String requestUrl) {
		this.requestUrl = requestUrl;
	}

	public String getRequestBody() {
		return requestBody;
	}

	public void setRequestBody(String requestBody) {
		this.requestBody = requestBody;
	}

	public String getRequestParameters() {
		return requestParameters;
	}

	public void setRequestParameters(String requestParameters) {
		this.requestParameters = requestParameters;
	}
}
